install.packages("sp")
install.packages("rgdal")
install.packages("maptools")
install.packages("rgeos")
install.packages("ggplot2")
install.packages("ggalt")
install.packages("ggthemes")
install.packages("jsonlite")
install.packages("purrr")
install.packages("viridis")
install.packages("scales")
install.packages("geojsonio")

library(geojsonio)
library(sp)
library(rgdal)
library(maptools)
library(rgeos)
library(ggplot2)
library(ggalt)
library(ggthemes)
library(jsonlite)
library(purrr)
library(viridis)
library(scales)

# https://stackoverflow.com/questions/35493585/r-import-html-json-map-file-to-use-for-heatmap

# set working directory #
setwd("F:\\awei\\Spring\\Data\\Replication data\\Submission files\\Data")
myMap <- readOGR("nielsentopo.json", "nielsen_dma", stringsAsFactors=FALSE,
                 verbose=FALSE)
myMap <- SpatialPolygonsDataFrame(gBuffer(myMap, byid=TRUE, width=0),
                                  data=myMap@data)
myMapDF <- fortify(myMap, region = "id")

df = read.csv("wage gap map.csv")
df$id = df$dmacode

gg = ggplot()
gg <- gg + geom_map(data=myMapDF, map=myMapDF,
                    aes(x=long, y=lat, map_id=id), size=.01, fill=NA)

gg <- gg + geom_map(data=df, map=myMapDF,
                    aes(fill=wage_gap, map_id=id), size=.01)

gg <- gg + theme_map()
gg = gg + theme(legend.position = "none")
gg
